package com.sczfdf.www.sda.gov.cn;

import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

import com.jayway.jsonpath.JsonPath;
import com.sczfdf.www.sda.gov.cn.modo.GovDO;
import com.sczfdf.www.sda.gov.cn.modo.GovImageDO;
import com.sczfdf.www.sda.gov.cn.modo.PageDO;
import com.sczfdf.www.sda.gov.cn.pipeline.GovImagePipeline;
import com.sczfdf.www.sda.gov.cn.pipeline.GovPipeline;
import com.sczfdf.www.sda.gov.cn.pipeline.PagePipeline;

import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.model.OOSpider;
import us.codecraft.webmagic.selector.JsonPathSelector;

/**
 * 药监局化妆品备案网爬虫
 * 使用webmagic + c3p0
 * 开发遇到的问题
 * 1.DO
 * @author Administrator
 *
 */
public class App {
	public static boolean isSetRequest = true;
	//	private static Logger logger = Logger.getLogger(App.class); 

	public static void main( String[] args ) throws SQLException {
		httpdown httpdown = new httpdown();
//		httpdown.setProxyProvider(SimpleProxyProvider.from(new Proxy("http-dyn.abuyun.com", 9020, "H8X06B1310K67U1D", "EE7225B53BF072CB")));
		
		OOSpider.create(Site.me().setRetryTimes(3).setCycleRetryTimes(3).setTimeOut(5000).setSleepTime(0).setCharset("UTF-8"))
		.addPageModel(new PagePipeline(), PageDO.class)
		.addPageModel(new GovPipeline(), GovDO.class)
		.addPageModel(new GovImagePipeline(), GovImageDO.class)
		.addPipeline(new MyPipeline())
		.addRequest(PageDO.createRequest(1))
		.thread(5)
		.setDownloader(httpdown)
		.run();
	}

	/**
	 * 测试正则是否匹配url
	 */
	@Test
	public void t() {
		String page = "http://125.35.6.80:8080/ftba/itownet/fwAction.do?method=getAttachmentCpbz";
		Pattern targetPattern = Pattern.compile("[0-9A-Za-z\\.://\\?]+=getAttachmentCpbz");
		Matcher matcher = targetPattern.matcher(page);
		while(true) {
			if (matcher.find()) {
				System.out.println(matcher.group());
			}else break;
		}
		System.out.println(matcher.matches());
	}

	/**
	 * 测试JSONpath是否匹配
	 */
	@Test
	public void selectList() {
		String body = "{\"result\":[{\"context\":\"\",\"diaplayname\":\"\",\"endTime\":\"\",\"fileDesc\":\"\",\"fileName\":\"诗妮兰透亮雪肌原液——三膜美颜原液套立体图\",\"fileSize\":2665951,\"fileTitle\":\"\",\"fileType\":\".jpg\",\"filesizeb\":\"\",\"id\":\"ba568465d1c54b8b9ba997d26457831c\",\"isExcess\":\"\",\"isExport\":\"\",\"jybgType\":\"\",\"jyxmType\":\"\",\"md5Id\":\"\",\"oldId\":\"ba568465d1c54b8b9ba997d26457831c\",\"pdefid\":\"y-hp3bxqus-gs\",\"postComplete\":true,\"postedPercent\":\"100%\",\"postedTime\":\"2017-07-14 13:30:35\",\"processid\":\"2017071316453668yzt\",\"productName\":\"\",\"savePath\":\"attachs/2017-07/\",\"startTime\":\"\",\"sysType\":\"\",\"tableName\":\"\",\"title\":\"\",\"type\":\"\",\"uploadUid\":\"72785471X\",\"ysjlid\":\"\",\"zltype\":\"babacpbzlt\"},{\"context\":\"\",\"diaplayname\":\"\",\"endTime\":\"\",\"fileDesc\":\"\",\"fileName\":\"诗妮兰透亮雪肌原液——三膜美颜原液套平面图\",\"fileSize\":3560704,\"fileTitle\":\"\",\"fileType\":\".jpg\",\"filesizeb\":\"\",\"id\":\"9debe46ebef14d619769d2295f5cacf2\",\"isExcess\":\"\",\"isExport\":\"\",\"jybgType\":\"\",\"jyxmType\":\"\",\"md5Id\":\"\",\"oldId\":\"9debe46ebef14d619769d2295f5cacf2\",\"pdefid\":\"y-hp3bxqus-gs\",\"postComplete\":true,\"postedPercent\":\"100%\",\"postedTime\":\"2017-07-14 13:29:55\",\"processid\":\"2017071316453668yzt\",\"productName\":\"\",\"savePath\":\"attachs/2017-07/\",\"startTime\":\"\",\"sysType\":\"\",\"tableName\":\"\",\"title\":\"\",\"type\":\"\",\"uploadUid\":\"72785471X\",\"ysjlid\":\"\",\"zltype\":\"babacpbzpm\"},{\"context\":\"\",\"diaplayname\":\"\",\"endTime\":\"\",\"fileDesc\":\"\",\"fileName\":\"续展证明（诗妮兰化妆品）\",\"fileSize\":1516710,\"fileTitle\":\"\",\"fileType\":\".jpg\",\"filesizeb\":\"\",\"id\":\"e5cd453ac1144b2e9328a04a19010176\",\"isExcess\":\"\",\"isExport\":\"\",\"jybgType\":\"\",\"jyxmType\":\"\",\"md5Id\":\"\",\"oldId\":\"e5cd453ac1144b2e9328a04a19010176\",\"pdefid\":\"y-hp3bxqus-gs\",\"postComplete\":true,\"postedPercent\":\"100%\",\"postedTime\":\"2017-07-13 17:02:41\",\"processid\":\"2017071316453668yzt\",\"productName\":\"\",\"savePath\":\"attachs/2017-07/\",\"startTime\":\"\",\"sysType\":\"\",\"tableName\":\"\",\"title\":\"\",\"type\":\"\",\"uploadUid\":\"72785471X\",\"ysjlid\":\"\",\"zltype\":\"babaqtcl\"},{\"context\":\"\",\"diaplayname\":\"\",\"endTime\":\"\",\"fileDesc\":\"\",\"fileName\":\"诗妮兰商标注册证\",\"fileSize\":1228372,\"fileTitle\":\"\",\"fileType\":\".jpg\",\"filesizeb\":\"\",\"id\":\"e49c16ab18744991a1c43bcfb7bd798d\",\"isExcess\":\"\",\"isExport\":\"\",\"jybgType\":\"\",\"jyxmType\":\"\",\"md5Id\":\"\",\"oldId\":\"e49c16ab18744991a1c43bcfb7bd798d\",\"pdefid\":\"y-hp3bxqus-gs\",\"postComplete\":true,\"postedPercent\":\"100%\",\"postedTime\":\"2017-07-13 17:02:22\",\"processid\":\"2017071316453668yzt\",\"productName\":\"\",\"savePath\":\"attachs/2017-07/\",\"startTime\":\"\",\"sysType\":\"\",\"tableName\":\"\",\"title\":\"\",\"type\":\"\",\"uploadUid\":\"72785471X\",\"ysjlid\":\"\",\"zltype\":\"babaqtcl\"}],\"ssid\":\"qTPWpIE1upKNY0WMp1NxUQ||\\n\"}";
		String jsonPathStr = "$.ssid";

		JsonPathSelector jsonPathSele = new JsonPathSelector(jsonPathStr);
		String select = jsonPathSele.select(body);
		System.out.println(select);

		JsonPath jsonPath = JsonPath.compile(jsonPathStr);
		Object read = jsonPath.read(body);
		System.out.println(read);
		//        List<String> list = new ArrayList<String>();
		//        Object object = jsonPath.read(text);
		//        if (object == null) {
		//            return list;
		//        }
		//        if (object instanceof List) {
		//            List<Object> items = (List<Object>) object;
		//            for (Object item : items) {
		//                list.add(toString(item));
		//            }
		//        } else {
		//            list.add(toString(object));
		//        }
		//        return list;
	}

}
